﻿using System;
using System.Web.Mvc;
using System.Text.RegularExpressions;

using MSAcademic.v2.Infrastructure.Utility;
using MSAcademic.v2.Web.Models;

namespace MSAcademic.v2.Web.Controllers
{
    public partial class ArticleController : Controller
    {
        [HttpGet]
        public ActionResult ArticleProfile(int articleId, string articleTitle)
        {
            var viewModel = _articleViewModelFactory();
            viewModel.ArticleId = articleId;
            if((articleTitle != Text.ToUrlFriendlyString(viewModel.Article.Title)))
            {
                articleTitle =Text.ToUrlFriendlyString(viewModel.Article.Title);
                return RedirectToAction("ArticleProfile", new {articleId, articleTitle});

            }
            ViewData["UrlFriendlyTitle"] = Text.ToUrlFriendlyString(viewModel.Article.Title);
            ViewData["StrippedShortText"] = Regex.Replace(viewModel.Article.ShortText, @"<[^>]*>", String.Empty);
            ViewData["CurrentView"] =  "articles";
            return View(viewModel);
        }      
    } 
    
    public partial class ArticleController
    {
        private readonly Func<ArticleViewModel> _articleViewModelFactory;
        public ArticleController(Func<ArticleViewModel> articleViewModelFactory)
        {
            _articleViewModelFactory = articleViewModelFactory;
        }
    }
}
